﻿using TianYu.Core.Pdf.ContentElement;
using TianYu.Core.Pdf.Extension;

namespace TianYu.Core.Pdf.TemplateDocument.Line
{
    /// <summary>
    /// Panel
    /// </summary>
    public class PanelElement : AbstractElement
    {
        /// <summary> 边框线类型 </summary>
        public int Style { get; set; }

        /// <summary> 背景色：1白色， 2黑色 </summary>
        public int BackgroundColor { get; set; }

        /// <summary> 文字： 1白色， 2黑色 </summary>
        public int FontColor { get; set; }

        /// <summary> 边框 </summary>
        public BorderModel Border { get; set; }

        public override void Draw(PdfTemplateContent pdfContentByte)
        {
            var pdfContent = pdfContentByte.PdfWriter.DirectContent; 

            var lineStyle = CommonHelper.GetLineStyle(Style);
            if (lineStyle == LineStyle.Solid)
            {
                var rectTemp = new iTextSharp.text.Rectangle(X, Y, X + Width, Y + Height);

                if (BackgroundColor > 1)
                {//非白色，画背景
                    //画矩形：用于背景色
                    rectTemp.BackgroundColor = CommonHelper.GetBaseColor(BackgroundColor);
                    pdfContent.Rectangle(rectTemp);
                }

                var reduce = CommonHelper.LineWidthReduce;
                rectTemp.BorderWidthLeft = Border.WidthLeft / reduce;
                rectTemp.BorderWidthRight = Border.WidthRight / reduce;
                rectTemp.BorderWidthBottom = Border.WidthBottom / reduce;
                rectTemp.BorderWidthTop = Border.WidthTop / reduce;

                pdfContent.Rectangle(rectTemp);
                pdfContent.Stroke();
            }
            else
            {
                //非实线，需要独单描边（画单线）
                if (BackgroundColor > 1)
                {//非白色，画背景
                    //画矩形：用于背景色
                    var rectTemp = new iTextSharp.text.Rectangle(X - 0.5f, Y - 0.5f, X + Width + 0.5f, Y + Height + 0.5f);//0.5》处理包括边界问题
                    rectTemp.BackgroundColor = CommonHelper.GetBaseColor(BackgroundColor);
                    pdfContent.Rectangle(rectTemp);
                }
                //画边框
                //下边框线
                pdfContent.WriteSolidLine(X, Y, X + Width, Y, lineStyle, Border.WidthBottom);
                //上边框
                pdfContent.WriteSolidLine(X, Y + Height, X + Width, Y + Height, lineStyle, Border.WidthTop);
                //左
                pdfContent.WriteSolidLine(X, Y, X, Y + Height, lineStyle, Border.WidthLeft);
                //右
                pdfContent.WriteSolidLine(X + Width, Y, X + Width, Y + Height, lineStyle, Border.WidthRight);
            }
        }
    }
}
